# Author: mosra <mosra@centrum.cz>
pkgname=magnum
pkgver=dev
pkgrel=1
pkgdesc="C++11/C++14 graphics middleware for games and data visualization"
arch=('i686' 'x86_64')
url="https://magnum.graphics"
license=('MIT')
depends=('corrade' 'openal' 'sdl2' 'glfw' 'vulkan-icd-loader')
makedepends=('cmake' 'ninja')
checkdepends=('vulkan-swrast')
options=('!strip' '!buildflags')
provides=('magnum-git')

_rootdir=$startdir/../../

build() {
    mkdir -p "$_rootdir/build"
    cd "$_rootdir/build"

    # RelWithDebInfo is enabled but not built -- it's meant for profiling from
    # within the build dir
    cmake .. \
        -DCMAKE_CONFIGURATION_TYPES="Debug;Release;RelWithDebInfo" \
        -DCMAKE_CROSS_CONFIGS=all \
        -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O2 -g -DNDEBUG -fno-omit-frame-pointer" \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DMAGNUM_WITH_AUDIO=ON \
        -DMAGNUM_WITH_VK=ON \
        -DMAGNUM_WITH_GLFWAPPLICATION=ON \
        -DMAGNUM_WITH_GLXAPPLICATION=ON \
        -DMAGNUM_WITH_SDL2APPLICATION=ON \
        -DMAGNUM_WITH_XEGLAPPLICATION=ON \
        -DMAGNUM_WITH_WINDOWLESSGLXAPPLICATION=ON \
        -DMAGNUM_WITH_WINDOWLESSEGLAPPLICATION=ON \
        -DMAGNUM_WITH_EGLCONTEXT=ON \
        -DMAGNUM_WITH_GLXCONTEXT=ON \
        -DMAGNUM_WITH_OPENGLTESTER=ON \
        -DMAGNUM_WITH_ANYAUDIOIMPORTER=ON \
        -DMAGNUM_WITH_ANYIMAGECONVERTER=ON \
        -DMAGNUM_WITH_ANYIMAGEIMPORTER=ON \
        -DMAGNUM_WITH_ANYSCENECONVERTER=ON \
        -DMAGNUM_WITH_ANYSCENEIMPORTER=ON \
        -DMAGNUM_WITH_ANYSHADERCONVERTER=ON \
        -DMAGNUM_WITH_MAGNUMFONT=ON \
        -DMAGNUM_WITH_MAGNUMFONTCONVERTER=ON \
        -DMAGNUM_WITH_OBJIMPORTER=ON \
        -DMAGNUM_WITH_TGAIMAGECONVERTER=ON \
        -DMAGNUM_WITH_TGAIMPORTER=ON \
        -DMAGNUM_WITH_WAVAUDIOIMPORTER=ON \
        -DMAGNUM_WITH_DISTANCEFIELDCONVERTER=ON \
        -DMAGNUM_WITH_FONTCONVERTER=ON \
        -DMAGNUM_WITH_IMAGECONVERTER=ON \
        -DMAGNUM_WITH_SCENECONVERTER=ON \
        -DMAGNUM_WITH_SHADERCONVERTER=ON \
        -DMAGNUM_WITH_GL_INFO=ON \
        -DMAGNUM_WITH_VK_INFO=ON \
        -DMAGNUM_WITH_AL_INFO=ON \
        -DMAGNUM_BUILD_TESTS=ON \
        -DMAGNUM_BUILD_GL_TESTS=ON \
        -DMAGNUM_BUILD_VK_TESTS=ON \
        -G "Ninja Multi-Config"
    ninja all:Debug all:Release
}

check() {
    cd "$_rootdir/build"
    export CORRADE_TEST_SKIP_BENCHMARKS=ON
    export CORRADE_TEST_COLOR=ON

    for config in Debug Release; do
        ctest -C $config --output-on-failure -j9
        MAGNUM_DISABLE_EXTENSIONS="GL_ARB_invalidate_subdata GL_ARB_multi_bind GL_ARB_robustness GL_ARB_separate_shader_objects GL_ARB_texture_storage GL_ARB_texture_storage_multisample GL_ARB_shading_language_420pack GL_ARB_explicit_uniform_location GL_ARB_explicit_attrib_location GL_ARB_texture_filter_anisotropic" ctest -C $config --output-on-failure -j9 -R GLTest
        MAGNUM_DISABLE_EXTENSIONS="GL_ARB_direct_state_access" ctest -C $config --output-on-failure -j9 -R GLTest
        MAGNUM_DISABLE_EXTENSIONS="GL_ARB_get_texture_sub_image" ctest -C $config --output-on-failure -j9 -R GLTest
        MAGNUM_DISABLE_EXTENSIONS="GL_ARB_vertex_array_object" ctest -C $config --output-on-failure -j9 -R GLTest
        MAGNUM_DISABLE_EXTENSIONS="GL_ARB_uniform_buffer_object GL_ARB_shader_storage_buffer_object" ctest -C $config --output-on-failure -j9 -R GLTest
        MAGNUM_DISABLE_EXTENSIONS="GL_KHR_debug" ctest -C $config --output-on-failure -j9 -R GLTest

        # Run all Vulkan tests with llvmpipe as well
        # Keep in sync with PKGBUILD-coverage and package/ci/unix-desktop-vulkan.sh
        MAGNUM_DEVICE=cpu ctest -C $config --output-on-failure -j9 -R VkTest
        for device in "" cpu; do
            MAGNUM_DEVICE=$device MAGNUM_VULKAN_VERSION=1.0 ctest -C $config --output-on-failure -j9 -R VkTest
            MAGNUM_DEVICE=$device MAGNUM_DISABLE_EXTENSIONS="VK_KHR_get_physical_device_properties2 VK_KHR_get_memory_requirements2 VK_KHR_bind_memory2 VK_KHR_create_renderpass2 VK_KHR_copy_commands2 VK_KHR_maintenance1 VK_KHR_multiview VK_KHR_maintenance2" MAGNUM_VULKAN_VERSION=1.0 ctest -C $config --output-on-failure -j9 -R VkTest
        done
    done
}

package() {
    cd "$_rootdir/build"
    DESTDIR="$pkgdir/" ninja install:Debug

    # Prevent the Release build from overwriting Debug binaries
    for i in $(ls $pkgdir/usr/bin/magnum-*); do
        mv ${i}{,d}
    done

    DESTDIR="$pkgdir/" ninja install/strip:Release
}
